<?php
require_once('../inc/config.db.php');
session_start();

if ( $_SESSION['manager']['is_admin'] && is_numeric($_GET['manager']) )
{
    $query = sprintf
    (
	'SELECT date_format(min(date_time),\'%%d.%%m.%%Y, %%H:%%i\') AS `time`,'
		.'(SELECT TIME_TO_SEC(time)-TIME_TO_SEC(\'09:15:00\')),'
		.'`reason`'
    	    .' FROM `crm`.`arrival_time`'
    	    .' WHERE `type`=\'login\''
    		.' AND `manager_id`=%d'
    		.' AND `date_time` BETWEEN \'%s\' AND \'%s 23:59:59\''
    	    .' GROUP BY DATE_FORMAT(date_time,\'%%Y-%%m-%%d\')',
    	mysql_escape_string($_GET['manager']),
    	mysql_escape_string($_GET['start']),
    	mysql_escape_string($_GET['stop'])
    );

    $result = mysql_query($query);

    while ( $row = mysql_fetch_row($result) )
    {
	$data[] = array($row);
    }

    print json_encode(array('row'=>$data));
}
?>
